package com.hlxj.license.server.controller;

import com.hlxj.license.server.model.LicenseInfo;
import com.hlxj.license.server.service.LicenseInfoService;
import com.hlxj.license.server.util.DateUtil;
import com.hlxj.license.server.util.FileUtil;
import com.hlxj.license.server.util.SendMailUtil;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.sql.Timestamp;
import java.util.UUID;

/**
 * @功能:【UuidController 注册时获取uuid接口】
 * @作者:代守诚
 * @日期:2019/5/20
 * @时间:23:02
 */
@RestController
@RequestMapping("/licenseServer")
public class UuidController {

    private static final Logger logger = LogManager.getLogger(UuidController.class);

    @Autowired
    private LicenseInfoService licenseInfoService;

    @RequestMapping(value = "/generate/uuid", method = RequestMethod.POST)
    public String generateUUID(Integer month, HttpServletRequest request, HttpServletResponse response) {
        String uuid = UUID.randomUUID().toString().replace("-", "");
        logger.info("***************UUID************************" + uuid);
        LicenseInfo licenseInfo = new LicenseInfo();
        licenseInfo.setUuid(uuid);
        licenseInfo.setCreateTime(new Timestamp(System.currentTimeMillis()));
        String registerTime = DateUtil.getCurrentTime();
        String endTime = DateUtil.getNewDateByYear(registerTime, month);
        licenseInfo.setEndTime(DateUtil.getTimestamp(endTime));

        LicenseInfo lic = licenseInfoService.addLicenseInfo(licenseInfo);

        return lic.getUuid();
    }

    @RequestMapping(value = "/generate/uuidByEmail", method = RequestMethod.POST)
    public boolean generateUUIDByEmail(Integer month, String email, HttpServletRequest request, HttpServletResponse response) {
        String uuid = UUID.randomUUID().toString().replace("-", "");
        logger.info("***************UUID************************" + uuid);
        LicenseInfo info = licenseInfoService.selectLicenseInfo(email);
        if (info != null && !StringUtils.isEmpty(info.getUuid())) {
            return false;
        }

        LicenseInfo licenseInfo = new LicenseInfo();
        licenseInfo.setUuid(uuid);
        licenseInfo.setEmail(email);
        licenseInfo.setCreateTime(new Timestamp(System.currentTimeMillis()));
        String registerTime = DateUtil.getCurrentTime();
        String endTime = DateUtil.getNewDateByYear(registerTime, month);
        licenseInfo.setEndTime(DateUtil.getTimestamp(endTime));

        LicenseInfo lic = licenseInfoService.addLicenseInfo(licenseInfo);

        File file = new File(FileUtil.getBasePath() +  "\\publicKey.txt");
        String monthStr = month + "个月，到期时间为" + endTime;
        try{
            SendMailUtil.getInstance().sendMailFile(email, file, lic.getUuid(), monthStr);
            return true;
        }catch (Exception e) {
            logger.error(e.getMessage());
        }

        return false;
    }

}
